package com.shaozi.nc.server.rpc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shaozi.data.rpc.base.RpcResultBean;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.bean.resp.ReqListSecretaryBean;
import com.shaozi.nc.bean.resp.ReqSecretaryTypeBadgeBean;
import com.shaozi.nc.bean.resp.ReqSecretaryUnreadMessageTotalBean;
import com.shaozi.nc.bean.resp.RespOperationTypeBadgeBean;
import com.shaozi.nc.bean.resp.RespOperationUnreadMessageTotalBean;
import com.shaozi.nc.data.model.BriefReportModel;
import com.shaozi.nc.data.model.OperationModel;
import com.shaozi.nc.data.model.OperationResponseModel;
import com.shaozi.nc.data.model.SecretaryModel;
import com.shaozi.nc.manage.secretary.BaseSecretaryManage;
import com.shaozi.nc.manage.secretary.OperationManage;
import com.shaozi.nc.mq.bean.operation.OperationMsgBean;
import com.shaozi.nc.utils.OperationHelper;
import com.shaozi.utils.common.BeanUtils;

/**
 * 目前与SecretaryRpcServer 相同
 * 
 * @author lyx
 *
 */
@Component("operationRpcServer")
public class OperationRpcServer extends BaseSecretaryRpcServer<OperationModel> {

	public OperationRpcServer() {

	}

	@Autowired
	private OperationManage operationManage;

	@Override
	public BaseSecretaryManage<OperationModel> onManange() {
		return operationManage;
	}

	@Override
	public RpcResultBean getTypeBadge(ReqSecretaryTypeBadgeBean reqBean) throws SzException {
		RpcResultBean ret = new RpcResultBean();
		Map<String, RespOperationTypeBadgeBean> resps = onManange().getTypeBadge(reqBean);
		if ((resps == null) || (resps.isEmpty()))
			return ret;
		for (RespOperationTypeBadgeBean resp : resps.values()) {
			if (resp.getOperation() != null) {
				OperationMsgBean msgBean = OperationHelper.toMsgBean(resp.getOperation(), reqBean.getUserId());
				if (msgBean != null) {
					resp.setMsgBean(msgBean);
				}
				resp.setOperation(null);
			}
		}
		ret.setData(resps);
		return ret;
	}

	@Override
	public RpcResultBean unreadMessageTotal(ReqSecretaryUnreadMessageTotalBean reqBean) throws SzException {
		RpcResultBean ret = new RpcResultBean();
		RespOperationUnreadMessageTotalBean resp = (RespOperationUnreadMessageTotalBean) onManange()
				.unreadMessageTotal(reqBean);
		if (resp == null)
			return ret;

		OperationMsgBean msgBean = OperationHelper.toMsgBean(resp.getOperation(), reqBean.getUserId());
		if (msgBean != null) {
			String title = msgBean.getTitle();
			msgBean.setContent(title);
			msgBean.setTitle(BeanUtils.getConfig().getValue("operator.titile", "资讯"));
			resp.setMsgBean(msgBean);
		}
		resp.setOperation(null);
		ret.setData(resp);
		return ret;
	}

	@Override
	public RpcResultBean listUpdate(ReqListSecretaryBean reqBean) {
		RpcResultBean ret = new RpcResultBean();
		OperationResponseModel model = new OperationResponseModel();
		List<SecretaryModel> secretarys = null;
		boolean isInit = (reqBean.getTimestamp() == null) || (reqBean.getTimestamp() == 0l);
		try {
			secretarys = onManange().listUpdateObjects(reqBean);
			setBaseSecretaryResponseModel(model, isInit, secretarys, reqBean);
		} catch (SzException e) {
			ret.setCode(e.getCode());
			ret.setMsg(e.getMessage());
			return ret;
		}
		if ((secretarys == null) || (secretarys.isEmpty())) {
			return ret;
		}

		List<OperationMsgBean> msgBeans = new ArrayList<>();
		secretarys.forEach((secretary) -> {
			msgBeans.addAll(OperationHelper.toMsgBean((OperationModel) secretary));
		});
		model.setData(msgBeans);
		ret.setData(model);
		return ret;
	}

}
